﻿using System;
using System.Windows;
using Sinacor.OMM.Util;

namespace Sinacor.OMM.Deployment.Setup
{
	public partial class Instalacao
	{
        #region [ Properties ]

        Conexao _conexao;

        #endregion

        #region [ Constructors ]

        public Instalacao()
		{
			this.InitializeComponent();
            this.ResizeMode = ResizeMode.NoResize;
        }

        public Instalacao(Conexao conexao)
        {
            _conexao = conexao;
            this.InitializeComponent();
            this.ResizeMode = ResizeMode.NoResize;
        }

        #endregion

        #region [ Events ]

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            if (SetupBase.ShowExitConfirm())
                SetupBase.CloseMainWindow();
        }        

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            _conexao.Show();
            this.Hide();
        }

        private void btnInstallReceptor_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnInstallReceptor.Visibility = Visibility.Hidden;

                loader01.Visibility = Visibility.Visible;

                LedReceptorOK.Visibility = Visibility.Hidden;
                LedReceptorNOK.Visibility = Visibility.Hidden;

                LblReceptorUninstallSucesso.Visibility = Visibility.Hidden;
                LblInstalandoReceptor.Visibility = Visibility.Visible;
                LblDesinstalandoReceptor.Visibility = Visibility.Hidden;
                LblErroReceptor.Visibility = Visibility.Hidden;
                LblErroUninstallReceptor.Visibility = Visibility.Hidden;
                LblReceptorSucesso.Visibility = Visibility.Hidden;                              

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.Receptor, Parameter.Enums.SetupMode.Install, InstallWSAsyncCallBack, installWSDispatcher);                
            }
            catch (Exception ex)
            {

            }                     
        }

        private void btnUninstallReceptor_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnUninstallReceptor.Visibility = Visibility.Hidden;

                loader01.Visibility = Visibility.Visible;

                LedReceptorOK.Visibility = Visibility.Hidden;
                LedReceptorNOK.Visibility = Visibility.Hidden;

                LblReceptorSucesso.Visibility = Visibility.Hidden;
                LblInstalandoReceptor.Visibility = Visibility.Hidden;
                LblDesinstalandoReceptor.Visibility = Visibility.Visible;
                LblErroReceptor.Visibility = Visibility.Hidden;
                LblErroUninstallReceptor.Visibility = Visibility.Hidden;
                LblReceptorUninstallSucesso.Visibility = Visibility.Hidden;                              

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.Receptor, Parameter.Enums.SetupMode.Uninstall, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }                     
        }

        private void btnInstallRouter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnInstallRouter.Visibility = Visibility.Hidden;

                loader02.Visibility = Visibility.Visible;

                LedRoteadorOK.Visibility = Visibility.Hidden;
                LedRoteadorNOK.Visibility = Visibility.Hidden;                

                LblRoteadorUninstallSucesso.Visibility = Visibility.Hidden;
                LblRoteadorInstalando.Visibility = Visibility.Visible;
                LblRoteadorDesinstalando.Visibility = Visibility.Hidden;
                LblRoteadorErro.Visibility = Visibility.Hidden;
                LblRoteadorUnistallErro.Visibility = Visibility.Hidden;
                LblRoteadorSucesso.Visibility = Visibility.Hidden;                

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.Router, Parameter.Enums.SetupMode.Install, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }         
        }

        private void btnUninstallRouter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnUninstallRouter.Visibility = Visibility.Hidden;

                loader02.Visibility = Visibility.Visible;

                LedRoteadorOK.Visibility = Visibility.Hidden;
                LedRoteadorNOK.Visibility = Visibility.Hidden;

                LblRoteadorSucesso.Visibility = Visibility.Hidden;
                LblRoteadorInstalando.Visibility = Visibility.Hidden;
                LblRoteadorDesinstalando.Visibility = Visibility.Visible;
                LblRoteadorErro.Visibility = Visibility.Hidden;
                LblRoteadorUnistallErro.Visibility = Visibility.Hidden;
                LblRoteadorUninstallSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.Router, Parameter.Enums.SetupMode.Uninstall, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }                     
        }

        private void btnInstallDumper_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnInstallDumper.Visibility = Visibility.Hidden;

                loader03.Visibility = Visibility.Visible;

                LedDumperOK.Visibility = Visibility.Hidden;
                LedDumperNOK.Visibility = Visibility.Hidden;

                LblDumperUninstallSucesso.Visibility = Visibility.Hidden;
                LblDumperInstalando.Visibility = Visibility.Visible;
                LblDumperDesinstalando.Visibility = Visibility.Hidden;
                LblDumperErro.Visibility = Visibility.Hidden;
                LblDumperUninstallErro.Visibility = Visibility.Hidden;
                LblDumperSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.Dumper, Parameter.Enums.SetupMode.Install, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }         
        }

        private void btnUninstallDumper_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnUninstallDumper.Visibility = Visibility.Hidden;

                loader03.Visibility = Visibility.Visible;

                LedDumperOK.Visibility = Visibility.Hidden;
                LedDumperNOK.Visibility = Visibility.Hidden;

                LblDumperSucesso.Visibility = Visibility.Hidden;
                LblDumperInstalando.Visibility = Visibility.Hidden;
                LblDumperDesinstalando.Visibility = Visibility.Visible;
                LblDumperErro.Visibility = Visibility.Hidden;
                LblDumperUninstallErro.Visibility = Visibility.Hidden;
                LblDumperUninstallSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.Dumper, Parameter.Enums.SetupMode.Uninstall, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }
        }

        private void btnInstallHFTProcess_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnInstallHFTProcess.Visibility = Visibility.Hidden;

                loader04.Visibility = Visibility.Visible;

                LedHFTOK.Visibility = Visibility.Hidden;
                LedHFTNOK.Visibility = Visibility.Hidden;

                LblHFTUninstallSucesso.Visibility = Visibility.Hidden;
                LblHFTInstalando.Visibility = Visibility.Visible;
                LblHFTDesinstalando.Visibility = Visibility.Hidden;
                LblHFTErro.Visibility = Visibility.Hidden;
                LblHFTUninstallErro.Visibility = Visibility.Hidden;
                LblHFTSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.HFTProcess, Parameter.Enums.SetupMode.Install, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }         
        }

        private void btnUninstallHFTProcess_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnUninstallHFTProcess.Visibility = Visibility.Hidden;

                loader04.Visibility = Visibility.Visible;

                LedHFTOK.Visibility = Visibility.Hidden;
                LedHFTNOK.Visibility = Visibility.Hidden;

                LblHFTSucesso.Visibility = Visibility.Hidden;
                LblHFTInstalando.Visibility = Visibility.Hidden;
                LblHFTDesinstalando.Visibility = Visibility.Visible;
                LblHFTErro.Visibility = Visibility.Hidden;
                LblHFTUninstallErro.Visibility = Visibility.Hidden;
                LblHFTUninstallSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.HFTProcess, Parameter.Enums.SetupMode.Uninstall, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }
        }

        private void btnInstallOrderExecutor_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnInstallOrderExecutor.Visibility = Visibility.Hidden;

                loader05.Visibility = Visibility.Visible;

                LedOrderExecutorOK.Visibility = Visibility.Hidden;
                LedOrderExecutorNOK.Visibility = Visibility.Hidden;

                LblOrderExecutorUninstallSucesso.Visibility = Visibility.Hidden;
                LblOrderExecutorInstalando.Visibility = Visibility.Visible;
                LblOrderExecutorDesinstalando.Visibility = Visibility.Hidden;
                LblOrderExecutorErro.Visibility = Visibility.Hidden;
                LblOrderExecutorUninstallErro.Visibility = Visibility.Hidden;
                LblOrderExecutorSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.OrderExecutor, Parameter.Enums.SetupMode.Install, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }         
        }

        private void btnUninstallOrderExecutor_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnUninstallOrderExecutor.Visibility = Visibility.Hidden;

                loader05.Visibility = Visibility.Visible;

                LedOrderExecutorOK.Visibility = Visibility.Hidden;
                LedOrderExecutorNOK.Visibility = Visibility.Hidden;

                LblOrderExecutorSucesso.Visibility = Visibility.Hidden;
                LblOrderExecutorInstalando.Visibility = Visibility.Hidden;
                LblOrderExecutorDesinstalando.Visibility = Visibility.Visible;
                LblOrderExecutorErro.Visibility = Visibility.Hidden;
                LblOrderExecutorUninstallErro.Visibility = Visibility.Hidden;
                LblOrderExecutorUninstallSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.OrderExecutor, Parameter.Enums.SetupMode.Uninstall, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }
        }

        private void btnInstallHealthMonitor_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnInstallHealthMonitor.Visibility = Visibility.Hidden;

                loader06.Visibility = Visibility.Visible;

                LedHealthMonitorOK.Visibility = Visibility.Hidden;
                LedHealthMonitorNOK.Visibility = Visibility.Hidden;

                LblHealthMonitorUninstallSucesso.Visibility = Visibility.Hidden;
                LblHealthMonitorInstalando.Visibility = Visibility.Visible;
                LblHealthMonitorDesinstalando.Visibility = Visibility.Hidden;
                LblHealthMonitorErro.Visibility = Visibility.Hidden;
                LblHealthMonitorUninstallErro.Visibility = Visibility.Hidden;
                LblHealthMonitorSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.HealthMonitor, Parameter.Enums.SetupMode.Install, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }  
        }

        private void btnUninstallHealthMonitor_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnUninstallHealthMonitor.Visibility = Visibility.Hidden;

                loader06.Visibility = Visibility.Visible;

                LedHealthMonitorOK.Visibility = Visibility.Hidden;
                LedHealthMonitorNOK.Visibility = Visibility.Hidden;

                LblHealthMonitorSucesso.Visibility = Visibility.Hidden;
                LblHealthMonitorInstalando.Visibility = Visibility.Hidden;
                LblHealthMonitorDesinstalando.Visibility = Visibility.Visible;
                LblHealthMonitorErro.Visibility = Visibility.Hidden;
                LblHealthMonitorUninstallErro.Visibility = Visibility.Hidden;
                LblHealthMonitorUninstallSucesso.Visibility = Visibility.Hidden;

                Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = new Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>(InstallWindowsService);
                installWSDispatcher.BeginInvoke(Parameter.Enums.Components.HealthMonitor, Parameter.Enums.SetupMode.Uninstall, InstallWSAsyncCallBack, installWSDispatcher);
            }
            catch (Exception ex)
            {

            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (SetupBase.ShowExitConfirm())
                SetupBase.CloseMainWindow();                
            else
                e.Cancel = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            LoadInitialControls();            
        }        

        #endregion

        #region [ Methods ]
    
        private void InstallComApplication()
        {
            Steps.InstallComPlus objComPlus = new Steps.InstallComPlus();
            objComPlus.InstallComOMMComponent(Environment.SystemDirectory + @"\" + "STMCom.dll");            
        }

        private void UninstallComApplication()
        {
            Steps.InstallComPlus objComPlus = new Steps.InstallComPlus();
            objComPlus.DeleteComOMMApplication();            
        }
   
        private void InstallWSAsyncCallBack(IAsyncResult ar)
        {
            Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool> installWSDispatcher = (Func<Parameter.Enums.Components, Parameter.Enums.SetupMode, bool>)ar.AsyncState;
            installWSDispatcher.EndInvoke(ar);   
        }

        private void SetControlsPosInstallWindowsService(Parameter.Enums.Components component, Parameter.Enums.SetupMode setupMode)
        {
            this.Dispatcher.Invoke(
                (Action)delegate()
                {
                    if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                    {
                        loader01.Visibility = Visibility.Hidden;

                        LedReceptorOK.Visibility = Visibility.Visible;
                        LedReceptorNOK.Visibility = Visibility.Hidden;

                        LblInstalandoReceptor.Visibility = Visibility.Hidden;
                        LblDesinstalandoReceptor.Visibility = Visibility.Hidden;

                        LblErroReceptor.Visibility = Visibility.Hidden;
                        LblErroUninstallReceptor.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            btnUninstallReceptor.Visibility = Visibility.Visible;
                            btnInstallReceptor.Visibility = Visibility.Hidden;
                            LblReceptorUninstallSucesso.Visibility = Visibility.Hidden;                            
                            LblReceptorSucesso.Visibility = Visibility.Visible;                            
                        }
                        else
                        {
                            btnUninstallReceptor.Visibility = Visibility.Hidden;
                            btnInstallReceptor.Visibility = Visibility.Visible;
                            LblReceptorSucesso.Visibility = Visibility.Hidden;                            
                            LblReceptorUninstallSucesso.Visibility = Visibility.Visible;                            
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router)
                    {
                        loader02.Visibility = Visibility.Hidden;

                        LedRoteadorOK.Visibility = Visibility.Visible;
                        LedRoteadorNOK.Visibility = Visibility.Hidden;

                        LblRoteadorInstalando.Visibility = Visibility.Hidden;
                        LblRoteadorDesinstalando.Visibility = Visibility.Hidden;

                        LblRoteadorErro.Visibility = Visibility.Hidden;
                        LblRoteadorUnistallErro.Visibility = Visibility.Hidden;
                        
                        
                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            btnUninstallRouter.Visibility = Visibility.Visible;
                            btnInstallRouter.Visibility = Visibility.Hidden;
                            LblRoteadorUninstallSucesso.Visibility = Visibility.Hidden;
                            LblRoteadorSucesso.Visibility = Visibility.Visible;                            
                        }
                        else
                        {
                            btnUninstallRouter.Visibility = Visibility.Hidden;
                            btnInstallRouter.Visibility = Visibility.Visible;
                            LblRoteadorSucesso.Visibility = Visibility.Hidden;
                            LblRoteadorUninstallSucesso.Visibility = Visibility.Visible;                            
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper)
                    {
                        loader03.Visibility = Visibility.Hidden;

                        LedDumperOK.Visibility = Visibility.Visible;
                        LedDumperNOK.Visibility = Visibility.Hidden;

                        LblDumperInstalando.Visibility = Visibility.Hidden;
                        LblDumperDesinstalando.Visibility = Visibility.Hidden;

                        LblDumperErro.Visibility = Visibility.Hidden;
                        LblDumperUninstallErro.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            btnUninstallDumper.Visibility = Visibility.Visible;
                            btnInstallDumper.Visibility = Visibility.Hidden;
                            LblDumperUninstallSucesso.Visibility = Visibility.Hidden;
                            LblDumperSucesso.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            btnUninstallDumper.Visibility = Visibility.Hidden;
                            btnInstallDumper.Visibility = Visibility.Visible;
                            LblDumperSucesso.Visibility = Visibility.Hidden;
                            LblDumperUninstallSucesso.Visibility = Visibility.Visible;
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess)
                    {
                        loader04.Visibility = Visibility.Hidden;

                        LedHFTOK.Visibility = Visibility.Visible;
                        LedHFTNOK.Visibility = Visibility.Hidden;

                        LblHFTInstalando.Visibility = Visibility.Hidden;
                        LblHFTDesinstalando.Visibility = Visibility.Hidden;

                        LblHFTErro.Visibility = Visibility.Hidden;
                        LblHFTUninstallErro.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            btnUninstallHFTProcess.Visibility = Visibility.Visible;
                            btnInstallHFTProcess.Visibility = Visibility.Hidden;
                            LblHFTUninstallSucesso.Visibility = Visibility.Hidden;
                            LblHFTSucesso.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            btnUninstallHFTProcess.Visibility = Visibility.Hidden;
                            btnInstallHFTProcess.Visibility = Visibility.Visible;
                            LblHFTSucesso.Visibility = Visibility.Hidden;
                            LblHFTUninstallSucesso.Visibility = Visibility.Visible;
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor)
                    {
                        loader05.Visibility = Visibility.Hidden;

                        LedOrderExecutorOK.Visibility = Visibility.Visible;
                        LedOrderExecutorNOK.Visibility = Visibility.Hidden;

                        LblOrderExecutorInstalando.Visibility = Visibility.Hidden;
                        LblOrderExecutorDesinstalando.Visibility = Visibility.Hidden;

                        LblOrderExecutorErro.Visibility = Visibility.Hidden;
                        LblOrderExecutorUninstallErro.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            btnUninstallOrderExecutor.Visibility = Visibility.Visible;
                            btnInstallOrderExecutor.Visibility = Visibility.Hidden;
                            LblOrderExecutorUninstallSucesso.Visibility = Visibility.Hidden;
                            LblOrderExecutorSucesso.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            btnUninstallOrderExecutor.Visibility = Visibility.Hidden;
                            btnInstallOrderExecutor.Visibility = Visibility.Visible;
                            LblOrderExecutorSucesso.Visibility = Visibility.Hidden;
                            LblOrderExecutorUninstallSucesso.Visibility = Visibility.Visible;
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor)
                    {
                        loader06.Visibility = Visibility.Hidden;

                        LedHealthMonitorOK.Visibility = Visibility.Visible;
                        LedHealthMonitorNOK.Visibility = Visibility.Hidden;

                        LblHealthMonitorInstalando.Visibility = Visibility.Hidden;
                        LblHealthMonitorDesinstalando.Visibility = Visibility.Hidden;

                        LblHealthMonitorErro.Visibility = Visibility.Hidden;
                        LblHealthMonitorUninstallErro.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            btnUninstallHealthMonitor.Visibility = Visibility.Visible;
                            btnInstallHealthMonitor.Visibility = Visibility.Hidden;
                            LblHealthMonitorUninstallSucesso.Visibility = Visibility.Hidden;
                            LblHealthMonitorSucesso.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            btnUninstallHealthMonitor.Visibility = Visibility.Hidden;
                            btnInstallHealthMonitor.Visibility = Visibility.Visible;
                            LblHealthMonitorSucesso.Visibility = Visibility.Hidden;
                            LblHealthMonitorUninstallSucesso.Visibility = Visibility.Visible;
                        }                        
                    }
                });
        }

        private void InstallUninstallMSMQ(Setup.Parameter.Enums.Components component, Setup.Parameter.Enums.SetupMode setupMode)
        {
            try
            {           
                new Setup.Steps.InstallMSMQ().Execute(GetQueueName(component), setupMode);                                    
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private bool InstallWindowsService(Parameter.Enums.Components component, Parameter.Enums.SetupMode setupMode)
        {
            try
            {
                Setup.Steps.InstallWindowsService installWindowsService = new Setup.Steps.InstallWindowsService();

                if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                {
                    if(setupMode == Sinacor.OMM.Deployment.Setup.Parameter.Enums.SetupMode.Install)
                        InstallComApplication();
                    else
                        UninstallComApplication();                    
                }                

                if (component != Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor)
                    InstallUninstallMSMQ(component, setupMode);

                installWindowsService.Execute(GetServiceName(component), GetServiceDisplayName(component), GetServiceExe(component), setupMode);

                SetControlsPosInstallWindowsService(component, setupMode);

                return true;
            }
            catch
            {
                ErroInstallUninstallWS(component, setupMode);
                return false;
            }
        }

        private void ErroInstallUninstallWS(Parameter.Enums.Components component, Parameter.Enums.SetupMode setupMode)
        {
            this.Dispatcher.Invoke(
                (Action)delegate()
                {
                    if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                    {                       
                        loader01.Visibility = Visibility.Hidden;

                        LedReceptorOK.Visibility = Visibility.Hidden;
                        LedReceptorNOK.Visibility = Visibility.Visible;

                        LblReceptorUninstallSucesso.Visibility = Visibility.Hidden;
                        LblReceptorSucesso.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            LblInstalandoReceptor.Visibility = Visibility.Hidden;                                
                            LblErroUninstallReceptor.Visibility = Visibility.Hidden;
                            LblErroReceptor.Visibility = Visibility.Visible;

                            btnUninstallReceptor.Visibility = Visibility.Hidden;
                            btnInstallReceptor.Visibility = Visibility.Visible;                        
                        }
                        else
                        {
                            LblDesinstalandoReceptor.Visibility = Visibility.Hidden;
                            LblErroUninstallReceptor.Visibility = Visibility.Visible;
                            LblErroReceptor.Visibility = Visibility.Hidden;

                            btnUninstallReceptor.Visibility = Visibility.Visible;
                            btnInstallReceptor.Visibility = Visibility.Hidden;                           
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router)
                    {
                        loader02.Visibility = Visibility.Hidden;

                        LedRoteadorOK.Visibility = Visibility.Hidden;
                        LedRoteadorNOK.Visibility = Visibility.Visible;

                        LblRoteadorUninstallSucesso.Visibility = Visibility.Hidden;
                        LblRoteadorSucesso.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            LblRoteadorInstalando.Visibility = Visibility.Hidden;
                            LblRoteadorUnistallErro.Visibility = Visibility.Hidden;
                            LblRoteadorErro.Visibility = Visibility.Visible;

                            btnUninstallRouter.Visibility = Visibility.Hidden;
                            btnInstallRouter.Visibility = Visibility.Visible;                         
                        }
                        else
                        {
                            LblRoteadorDesinstalando.Visibility = Visibility.Hidden;
                            LblRoteadorUnistallErro.Visibility = Visibility.Visible;
                            LblRoteadorErro.Visibility = Visibility.Hidden;

                            btnUninstallRouter.Visibility = Visibility.Visible;
                            btnInstallRouter.Visibility = Visibility.Hidden;                          
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper)
                    {
                        loader03.Visibility = Visibility.Hidden;

                        LedDumperOK.Visibility = Visibility.Hidden;
                        LedDumperNOK.Visibility = Visibility.Visible;

                        LblDumperUninstallSucesso.Visibility = Visibility.Hidden;
                        LblDumperSucesso.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            LblDumperInstalando.Visibility = Visibility.Hidden;
                            LblDumperUninstallErro.Visibility = Visibility.Hidden;
                            LblDumperErro.Visibility = Visibility.Visible;

                            btnUninstallDumper.Visibility = Visibility.Hidden;
                            btnInstallDumper.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            LblDumperDesinstalando.Visibility = Visibility.Hidden;
                            LblDumperUninstallErro.Visibility = Visibility.Visible;
                            LblDumperErro.Visibility = Visibility.Hidden;

                            btnUninstallDumper.Visibility = Visibility.Visible;
                            btnInstallDumper.Visibility = Visibility.Hidden;
                        }                        
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess)
                    {
                        loader04.Visibility = Visibility.Hidden;

                        LedHFTOK.Visibility = Visibility.Hidden;
                        LedHFTNOK.Visibility = Visibility.Visible;

                        LblHFTUninstallSucesso.Visibility = Visibility.Hidden;
                        LblHFTSucesso.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            LblHFTInstalando.Visibility = Visibility.Hidden;
                            LblHFTUninstallErro.Visibility = Visibility.Hidden;
                            LblHFTErro.Visibility = Visibility.Visible;

                            btnUninstallHFTProcess.Visibility = Visibility.Hidden;
                            btnInstallHFTProcess.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            LblHFTDesinstalando.Visibility = Visibility.Hidden;
                            LblHFTUninstallErro.Visibility = Visibility.Visible;
                            LblHFTErro.Visibility = Visibility.Hidden;

                            btnUninstallHFTProcess.Visibility = Visibility.Visible;
                            btnInstallHFTProcess.Visibility = Visibility.Hidden;
                        }                      
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor)
                    {
                        loader05.Visibility = Visibility.Hidden;

                        LedOrderExecutorOK.Visibility = Visibility.Hidden;
                        LedOrderExecutorNOK.Visibility = Visibility.Visible;

                        LblOrderExecutorUninstallSucesso.Visibility = Visibility.Hidden;
                        LblOrderExecutorSucesso.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            LblOrderExecutorInstalando.Visibility = Visibility.Hidden;
                            LblOrderExecutorUninstallErro.Visibility = Visibility.Hidden;
                            LblOrderExecutorErro.Visibility = Visibility.Visible;

                            btnUninstallOrderExecutor.Visibility = Visibility.Hidden;
                            btnInstallOrderExecutor.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            LblOrderExecutorDesinstalando.Visibility = Visibility.Hidden;
                            LblOrderExecutorUninstallErro.Visibility = Visibility.Visible;
                            LblOrderExecutorErro.Visibility = Visibility.Hidden;

                            btnUninstallOrderExecutor.Visibility = Visibility.Visible;
                            btnInstallOrderExecutor.Visibility = Visibility.Hidden;
                        }              
                    }
                    else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor)
                    {
                        loader06.Visibility = Visibility.Hidden;

                        LedHealthMonitorOK.Visibility = Visibility.Hidden;
                        LedHealthMonitorNOK.Visibility = Visibility.Visible;

                        LblHealthMonitorUninstallSucesso.Visibility = Visibility.Hidden;
                        LblHealthMonitorSucesso.Visibility = Visibility.Hidden;

                        if (setupMode == Parameter.Enums.SetupMode.Install)
                        {
                            LblHealthMonitorInstalando.Visibility = Visibility.Hidden;
                            LblHealthMonitorUninstallErro.Visibility = Visibility.Hidden;
                            LblHealthMonitorErro.Visibility = Visibility.Visible;

                            btnUninstallHealthMonitor.Visibility = Visibility.Hidden;
                            btnInstallHealthMonitor.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            LblHealthMonitorDesinstalando.Visibility = Visibility.Hidden;
                            LblHealthMonitorUninstallErro.Visibility = Visibility.Visible;
                            LblHealthMonitorErro.Visibility = Visibility.Hidden;

                            btnUninstallHealthMonitor.Visibility = Visibility.Visible;
                            btnInstallHealthMonitor.Visibility = Visibility.Hidden;
                        }              
                    }




                });
        }

        private void LoadInitialControls()
        {
            if (CheckSTMInstalled())
            {
                if (CheckWindowsServiceInstalled(Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor))
                {
                    btnInstallReceptor.Visibility = Visibility.Hidden;
                    btnUninstallReceptor.Visibility = Visibility.Visible;
                }
                else
                {
                    btnInstallReceptor.Visibility = Visibility.Visible;
                    btnUninstallReceptor.Visibility = Visibility.Hidden;
                }
            }
            else
            {
                btnInstallReceptor.Visibility = Visibility.Hidden;
                btnUninstallReceptor.Visibility = Visibility.Hidden;
                LedReceptorNOK.Visibility = Visibility.Visible;                
                LblErroSTMReceptor.Visibility = Visibility.Visible;
            }

            if (CheckWindowsServiceInstalled(Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router))
            {
                btnInstallRouter.Visibility = Visibility.Hidden;
                btnUninstallRouter.Visibility = Visibility.Visible;
            }
            else
            {
                btnInstallRouter.Visibility = Visibility.Visible;
                btnUninstallRouter.Visibility = Visibility.Hidden;
            }

            if (CheckWindowsServiceInstalled(Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper))
            {
                btnInstallDumper.Visibility = Visibility.Hidden;
                btnUninstallDumper.Visibility = Visibility.Visible;
            }
            else
            {
                btnInstallDumper.Visibility = Visibility.Visible;
                btnUninstallDumper.Visibility = Visibility.Hidden;
            }

            if (CheckWindowsServiceInstalled(Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess))
            {
                btnInstallHFTProcess.Visibility = Visibility.Hidden;
                btnUninstallHFTProcess.Visibility = Visibility.Visible;
            }
            else
            {
                btnInstallHFTProcess.Visibility = Visibility.Visible;
                btnUninstallHFTProcess.Visibility = Visibility.Hidden;
            }

            if (CheckWindowsServiceInstalled(Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.OrderExecutor))
            {
                btnInstallOrderExecutor.Visibility = Visibility.Hidden;
                btnUninstallOrderExecutor.Visibility = Visibility.Visible;
            }
            else
            {
                btnInstallOrderExecutor.Visibility = Visibility.Visible;
                btnUninstallOrderExecutor.Visibility = Visibility.Hidden;
            }

            if (CheckWindowsServiceInstalled(Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor))
            {
                btnInstallHealthMonitor.Visibility = Visibility.Hidden;
                btnUninstallHealthMonitor.Visibility = Visibility.Visible;
            }
            else
            {
                btnInstallHealthMonitor.Visibility = Visibility.Visible;
                btnUninstallHealthMonitor.Visibility = Visibility.Hidden;
            }
        }

        private string GetServiceName(Parameter.Enums.Components component)
        {
            return new Steps.InstallWindowsService().GetServiceName(component);
        }

        private string GetServiceDisplayName(Parameter.Enums.Components component)
        {
            return new Steps.InstallWindowsService().GetServiceDisplayName(component);
        }

        private string GetServiceExe(Parameter.Enums.Components component)
        {
            return new Steps.InstallWindowsService().GetServiceExe(component);
        }

        private string GetQueueName(Parameter.Enums.Components component)
        {
            return new Steps.InstallMSMQ().GetQueueName(component);
        }

        private bool CheckWindowsServiceInstalled(Parameter.Enums.Components component)
        {   
            return new Steps.InstallWindowsService().VerifyInstallService(GetServiceName(component));
        }

        private bool CheckSTMInstalled()
        {
            return STM.CheckInstalled();
        }

        #endregion  
    }
}